<?php

namespace app\admin\model\pes;

use app\admin\model\pes\freight\Price;
use think\Model;
use think\model\relation\HasMany;


class Locations extends Model
{

    

    

    // 表名
    protected $name = 'pes_locations';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];

    /**
     * @return HasMany
     */
    public function freightPrices(): HasMany
    {
        return $this->hasMany(Price::class, 'origin_id');
    }

    /**
     * @return HasMany
     */
    public function childLocations(): HasMany
    {
        return $this->hasMany(Locations::class, 'parent_id');
    }
    public function getTypeList()
    {
        return ['country' => __('国家'), 'state' => __('省'), 'city' => __('市'), 'region' => __('区'), 'area' => __('县'), 'street' => __('街道')];
    }

    public function getStatusList()
    {
        return ['0' => __('禁用'),'1'=>__('正常')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
